package org.clockworkmages.games.anno1186.generator;

import java.util.ArrayList;
import java.util.List;

import org.clockworkmages.games.anno1186.GameApplication;
import org.clockworkmages.games.anno1186.GameBeansContext;
import org.clockworkmages.games.anno1186.GameDataService;
import org.clockworkmages.games.anno1186.dao.FileUtil;
import org.clockworkmages.games.anno1186.dao.GameFilesList;
import org.clockworkmages.games.anno1186.dao.GameObjectsList;
import org.clockworkmages.games.anno1186.dao.SerializationUtil;
import org.clockworkmages.games.anno1186.generator.sets.BaseObjects;
import org.clockworkmages.games.anno1186.generator.sets.BasicItems;
import org.clockworkmages.games.anno1186.generator.sets.Beach;
import org.clockworkmages.games.anno1186.generator.sets.CharacterCreation;
import org.clockworkmages.games.anno1186.generator.sets.Jungle;
import org.clockworkmages.games.anno1186.generator.sets.Palos;
import org.clockworkmages.games.anno1186.generator.sets.PalosCardPlayer;
import org.clockworkmages.games.anno1186.generator.sets.PalosFence;
import org.clockworkmages.games.anno1186.generator.sets.PalosJuan;
import org.clockworkmages.games.anno1186.generator.sets.Tiles;

public class GenerateAndRun {

	// private Logger logger = LogManager.getLogger(this.getClass());

	public static final String ROOT_DIR = "C:/developer/workspace/anno1186/src/main/resources/data/";

	private List<String> gameObjectResourceFiles = new ArrayList<String>();

	public void run() {

		GameApplication.initContextBeans();
		GameApplication.createAndShowGUI();

		add(new BaseObjects());
		add(new Tiles());
		add(new BasicItems());
		add(new CharacterCreation());
		add(new Palos());
		add(new PalosJuan());
		add(new PalosCardPlayer());
		add(new PalosFence());
		add(new Beach());
		add(new Jungle());
		createResourceListFile();

		// javax.swing.SwingUtilities.invokeLater(new Runnable() {
		// public void run() {

		GameApplication.initTechnicalOptionsAndSituations();
		GameApplication.initGuiResources();
		GameApplication.startNewGame();
		// }
		// });

	}

	private void add(GameObjectGenerator gameObjectGenerator) {
		GameObjectsList gameObjects = gameObjectGenerator.generate();

		GameBeansContext.getBean(GameDataService.class).getGameData()
				.merge(gameObjects);

		String serializedXml = SerializationUtil.serializeGameData(gameObjects,
				true);
		FileUtil.write(ROOT_DIR + gameObjectGenerator.getFileName() + ".xml",
				serializedXml);
		gameObjectResourceFiles.add("data/" + gameObjectGenerator.getFileName()
				+ ".xml");
	}

	private void createResourceListFile() {
		GameFilesList gameFilesList = new GameFilesList();
		gameFilesList.setFiles(gameObjectResourceFiles);

		String serializedXml = SerializationUtil.serialize(gameFilesList, true);
		FileUtil.write(ROOT_DIR + "gameFiles" + ".xml", serializedXml);
	}

	public static void main(String[] args) {
		new GenerateAndRun().run();
	}

}
